/*
 * Copyright (C) 2010-2011 sunjumper@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package mfinder.impl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import mfinder.AbstractProxy;

/**
 * 默认方法代理类实现，封装了调用代理方法时异常的处理。
 */
public class DefaultProxy extends AbstractProxy {

    /**
     * 未指定方法及其对象的构造方法。
     */
    public DefaultProxy() {
    }

    /**
     * 指定方法及其对象的构造方法。
     *
     * @param method 指定的方法。
     * @param object 指定的对象。
     */
    public DefaultProxy(Method method, Object object) {
        super(method, object);
    }

    /**
     * 指定对象、方法及对象状态（是否单例）的构造方法。
     *
     * @param method 指定的方法。
     * @param object 指定的对象。
     * @param singleton 对象是否单例。
     */
    public DefaultProxy(Method method, Object object, boolean singleton) {
        super(method, object, singleton);
    }

    @Override
    protected Object invoke(Object... params) {
        Object res = null;
        try {
            res = method.invoke(object, params);
        } catch (IllegalAccessException e) {
            throw new InvocationProxyException(e, this);
        } catch (InvocationTargetException e) {
            throw new InvocationProxyException(e.getTargetException(), this);
        }
        return res;
    }
}
